package Z.dailyExercise.holiday;

import java.util.List;

public class _剑指Offer52两个链表的第一个公共节点 {
    public class ListNode {
      int val;
      ListNode next;
     ListNode(int x) {
         val = x;
         next = null;
      }
  }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int len1= getLenth(headA);
        int len2 = getLenth(headB);

        //int i=0,j=0;

        if (len1>=len2){
            int move = len1-len2;
            return helper(move,headA,headB);

        }else {
            int move = len2-len1;
            return helper(move,headB,headA);
        }

    }

    public int getLenth(ListNode head){
        int len =0;
        while (head != null){
            len++;
            head = head.next;
        }
        return len;
    }

    public ListNode helper(int move,ListNode longLN,ListNode shortLN){

        while (move>0){
            longLN = longLN.next;
            move--;
        }

        while (longLN != null ){

            if (longLN == shortLN){
                return longLN;
            }else {
                longLN = longLN.next;
                shortLN = shortLN.next;
            }
        }

        return null;

    }

}
